package com.neusoft.hifly.rpc.mobile.service.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.rpc.mobile.config.MobileConfig;
import com.neusoft.hifly.rpc.mobile.dao.IPushMobileDao;
import com.neusoft.hifly.rpc.mobile.domain.JPushModelObj;
import com.neusoft.hifly.rpc.mobile.domain.PushMobileVO;
import com.neusoft.hifly.rpc.mobile.service.IPushMobileService;

/**
 * Title: 消息推送中间件
 * <p>
 * Description: 站内信 API接口
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Service("pushMobileService")
public class PushMobileServiceImpl implements IPushMobileService {

	@Autowired(required = false)
	private IPushMobileDao pushMobileDao;
	@Autowired(required = false)
	private MobileConfig mobileConfig;

	/**
	 * 发布订阅模式
	 *
	 * @param pushObj
	 *            对象
	 */
	@EventListener
	private void handlerMobileEvent(final PushMobileVO pushObj) {

		final String title = pushObj.getTitle();
		final String message = pushObj.getMessage();
		final long sendUserLogicId = pushObj.getSendUserLogicId();
		final long receiveUserLogicId = pushObj.getReceiveUserLogicId();
		final Map<String, String> pushExtras = pushObj.getPushExtras();

		try {

			final String jiguangMasterSecret = mobileConfig.getJiguangMasterSecret();
			final String jiguangAppKey = mobileConfig.getJiguangAppKey();
			String jiguangPushType = mobileConfig.getJiguangPushType();
			if (StringUtils.isEmpty(jiguangPushType)) {
				jiguangPushType = JPushModelObj.PUSHTYPE_RELEASE;
			}

			// 通过WebService接口方式推送
			final JPushModelObj vo = new JPushModelObj(jiguangMasterSecret, jiguangAppKey);

			vo.setTitle(title);
			vo.setMessage(message);
			vo.setSendLogicId(sendUserLogicId);

			// 推送方式(alias或者tags，为空即为全部人员)
			vo.setPushWay(JPushModelObj.PUSHWAY_ALIAS);
			// 别名或者标签的字符串
			vo.setStringOfPushWay(receiveUserLogicId + ""); // 接收人ID

			// 扩展参数
			if (pushExtras != null && !pushExtras.isEmpty()) {
				vo.setPushExtras(pushExtras);
			}
			// 推送方式，是生产环境还是正式环境(release或者debug)
			vo.setPushType(jiguangPushType);

			this.sendMobilePushAlertFromJson(JSON.toJSONString(vo));
			Log.error("MOBIEL：向(" + receiveUserLogicId + ")的手机推送信息[" + title + "]");
		} catch (final Exception e) {
			Log.error("MOBIEL：向(" + receiveUserLogicId + ")的手机推送失败!" + e.getMessage());
			e.printStackTrace();
		}

	}

	/**
	 * 根据传入的json字符串来判断实现什么方式的推送通知内容为 ALERT
	 *
	 * @param jsonOfPushType
	 *            推送方式json串
	 * @return 返回值
	 */
	@Override
	public boolean sendMobilePushAlertFromJson(final String jsonOfPushType) {
		if (StringUtils.isEmpty(jsonOfPushType)) {
			return false;
		}

		final ApiResponse<Object> result = pushMobileDao.sendMobilePushAlertFromJson(jsonOfPushType);

		if (result.getCode() == ApiCode.ERROR.getCode()) {
			Log.error(result.getMessage());
			return false;
		}

		return true;

	}

}
